// License Notification
//======================================================
// Copyright (c) 2010 Channel Intelligence Inc.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// ======================================================




using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CIP4.Common;

namespace CIP4.Browser
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            try
            {
                SetupLog();
                WriteLog("\n\n========================");
                WriteLog("Starting CIP4.Browser");
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainBrowserForm());
                WriteLog("Ending CIP4.Browser");
                WriteLog("========================");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unexpected exeption occurred, details are in the log: " + ex.ToString());
                WriteLog(MajorEnum.Error, ex.ToString());
                WriteLog("Ending CIP4.Browser");
                WriteLog("========================");
            }
            finally
            {
                if (log != null)
                    log.Close();
            }

        }
        static StreamWriter log = null;
        public static void WriteLog(string message)
        {
            WriteLog(MajorEnum.Information, message);
        }
        public static void WriteLog(MajorEnum entryType, string message)
        {
            try
            {
                if (entryType < MajorEnum.Noise && !string.IsNullOrEmpty(message))
                    log.WriteLine(DateTime.Now.ToString("dd:HH:mm:ss.fff") + " - " + entryType.ToString() + " - " + message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private static void SetupLog()
        {
            int copyNumber = 0;
            while (true)
            {
                try
                {
                    string fileName =
                        Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) +
                        string.Format("\\{0}{1}.log", "CIP4.Browser", copyNumber == 0 ? string.Empty : "_" + copyNumber.ToString());
                    log = new StreamWriter(fileName, true);
                    break;
                }
                catch (IOException ex)
                {
                    copyNumber++;
                    if (copyNumber > 10)
                        throw new ApplicationException(string.Format("Unable to open the CIP4.Browser log file: {0}", ex.ToString()));
                }
            }
        }
    }
}
